<template>
  <div class="login-container">
    <div class="login-wrapper">
      <h2>登录页面</h2>
      <el-form v-model="loginForm">
        <el-form-item label="用户名">
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>

        <el-form-item label="密码">
          <el-input type="password" v-model="loginForm.password"></el-input>
        </el-form-item>

        <el-form-item>
          <el-radio v-model="loginForm.identity" label="0">学生</el-radio>
          <el-radio v-model="loginForm.identity" label="1">老师</el-radio>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleLogin()">登录</el-button>
          <el-button @click="registerDialogFormVisible = true">注册</el-button>
        </el-form-item>
      </el-form>
    </div>


    <el-dialog title="用户注册" :visible.sync="registerDialogFormVisible" :close-on-click-modal="false">

      <el-form v-model="registerForm" label-width="100px" >
        <el-form-item label="用户名">
          <el-input v-model="registerForm.username"></el-input>
        </el-form-item>

        <el-form-item label="密码">
          <el-input type="password" v-model="registerForm.password"></el-input>
        </el-form-item>

        <el-form-item>
          <el-radio v-model="registerForm.identity" label="0">学生</el-radio>
          <el-radio v-model="registerForm.identity" label="1">老师</el-radio>
        </el-form-item>

        <el-form-item>
          <el-button @click="closeRegisterDialog()">取 消</el-button>
          <el-button type="primary" @click="handleRegister()">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      registerDialogFormVisible: false,
      loginForm: {
        username: "",
        password: "",
        identity: "0",
      },
      registerForm: {
        username: "",
        password: "",
        identity: "0",
      },
    };
  },

  methods: {
    handleRegister(){
      this.axios.post("/user/register", this.registerForm).then((res) => {
        let type = res.data.code === 200 ? "success" : "error";
        this.$message({
          message: res.data.data.result,
          type: type,
        });
        if (res.data.code === 200) {
          this.$router.push("/");
        }
      });
      this.closeRegisterDialog();
    },
    closeRegisterDialog(){
      this.registerDialogFormVisible = false;
      this.resetRegisterForm();
    },
    handleLogin() {
      this.axios.post("/user/login", this.loginForm).then((res) => {
        let type = res.data.code === 200 ? "success" : "error";
        this.$message({
          message: res.data.data.result,
          type: type,
        });
        if (res.data.code === 200) {
          localStorage.setItem("loginUser",JSON.stringify(res.data.data.user));
          this.$router.push("/dashboard");
        }
      });
    },
    resetLoginForm() {
      this.loginForm.username = "";
      this.loginForm.password = "";
      this.loginForm.identity = "0";
    },
    resetRegisterForm(){
      this.registerForm.username = "";
      this.registerForm.password = "";
      this.registerForm.identity = "0";
    }
  },
};
</script>

<style>
.login-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.login-wrapper {
  width: 380px;
  border: 1px solid #ccc;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
</style>
